package cn.jgrzj.kaola.api.controller;

import cn.jgrzj.kaola.api.entity.KaolaCps;
import cn.jgrzj.kaola.api.service.KaolaService;
import cn.jgrzj.kaola.api.util.APIUtil;
import cn.quevo.common.lang.JSONUtils;
import cn.quevo.common.lang.StringUtils;
import cn.quevo.core.spring.SpringController;
import cn.quevo.core.spring.util.WebUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

@Controller("kaola")
public class KaolaController extends SpringController {

    @Resource(name = "kaolaService")
    private KaolaService kaolaService;
    @RequestMapping("/kaola")
    public String kaola(KaolaCps kaolaCps){
        TreeMap<String, String> parameterMap = new TreeMap<>();
        parameterMap.put ("timestamp", System.currentTimeMillis()+"") ;
        parameterMap.put("app_key", "753453177");
        parameterMap.put ("account" , StringUtils.getRandomNum(8)) ;
        parameterMap.put ("sign_method", "md5") ;
        //JBhwhJ30Wb
        String rightSign = APIUtil.createSign("JBhwhJ30Wb",parameterMap);
        String params = StringUtils.format("?timestamp={}&appKey={}&account={}&sign={}&signMethod=md5","{}",parameterMap.get("timestamp"),parameterMap.get("app_key"),parameterMap.get("account"),rightSign);
        if (kaolaCps == null) {
            kaolaCps = new KaolaCps();
        }
        kaolaCps.setIp(WebUtils.getIpAddr());
        kaolaCps.setCookie(getCookie());
        kaolaCps.setHeader(getHeaders());
        kaolaCps.setUserAgent(WebUtils.getHttpServletRequest().getHeader("User-Agent"));
        kaolaCps.setSourceTypeCd(StringUtils.isBlank(kaolaCps.getSourceTypeCd())?"10":kaolaCps.getSourceTypeCd());
        kaolaCps.setParams(JSONUtils.format(parameterMap));
        kaolaCps.setClickUrl("https://market.kaola.com/cooperation/bindIndexPage.html"+params);
        try {
            this.kaolaService.click(kaolaCps);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return WebUtils.toRedirect("https://market.kaola.com/cooperation/bindIndexPage.html"+params);
    }

    private String getCookie() {
        Cookie[] cookies = WebUtils.getHttpServletRequest().getCookies();
        if (cookies == null || cookies.length <= 0) {
            return "";
        }
        Map<String,String> cookieMap = new HashMap<>();
        for (Cookie cookie : cookies) {
            cookieMap.put(cookie.getName(),cookie.getValue());
        }
        return JSONUtils.format(cookieMap);
    }
    private String getHeaders(){
        Map<String,String> headerMap = new HashMap<>();
        Enumeration<?> enum1 = WebUtils.getHttpServletRequest().getHeaderNames();
        while (enum1.hasMoreElements()) {
            String key = (String) enum1.nextElement();
            String value = WebUtils.getHttpServletRequest().getHeader(key);
            headerMap.put(key,value);
        }
        return JSONUtils.format(headerMap);
    }
}
